//
//  CCStatusCellOriginalView.swift
//  CC-SinaWeibo
//
//  Created by Chenchen on 16/2/21.
//  Copyright © 2016年 Chenchen. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class CCStatusCellOriginalView: UIView {

    /// 添加约束对象属性,用于使用的记录
    var bottomConstaint: Constraint?
    
    // 添加模型属性
    var status: CCStatus? {
        didSet {
            // 绑定数据
            iconImageView.sd_setImageWithURL(status?.user?.profile_url)  // 用户头像
            nameLabel.text = status?.user?.name  // 用户名
            mbrank_image.image = status?.user?.mbrank_image  // 会员等级
            textLabel.text = status?.text  // 微博正文
            // 给配图视图的数组赋值
            pictureView.imageURLs = status?.imageURLs
            // 打印图片URL地址
//            print(status?.imageURLs)
            // 卸载老约束
            self.bottomConstaint?.uninstall()
            // 根据数据调整关系
            if let imageURLs = status?.imageURLs where imageURLs.count != 0 {
                // 说明有配图视图, 原创微博底部相对于配图视图进行约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
            } else {
                // 说明没有配图视图,原创微博底部相对于文字进行约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(textLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 背景颜色
        self.backgroundColor = UIColor.greenColor()
        // 设置UI
        setUpUI()
    }

    // 默认报错
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
// MARK: 设置UI界面
    private func setUpUI() {
        // 设置背景颜色
        sepView.backgroundColor = UIColor.redColor()
        // 添加子视图
        addSubview(sepView)  // sepView
        addSubview(iconImageView)  // 用户头像
        addSubview(nameLabel)  // 用户名称
        addSubview(timeLabel)  // 微博时间
        addSubview(sourceLabel)  // 微博来源
        addSubview(mbrank_image)  // 微博等级
        addSubview(verified_type_image)  // 用户认证类型
        addSubview(textLabel)  // 微博正文
        addSubview(pictureView)  // 模型图片
        
    // 设置约束
        /// sepView
        sepView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.height.equalTo(10)
        }
        
        /// 用户头像
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(StatusCellMargin)
            make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
            // 约束大小
//            make.size.equalTo(CGSize(width: 40, height: 40))
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        /// 用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMargin)
            make.top.equalTo(iconImageView.snp_top)
        }
        
        /// 微博时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconImageView.snp_bottom)
            make.left.equalTo(iconImageView.snp_right).offset(StatusCellMargin)
        }
        
        /// 微博来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(timeLabel.snp_top)
        }
        
        /// 微博会员(等级)
        mbrank_image.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        /// 用户认证类型(在图片内的右下角)
        verified_type_image.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(iconImageView.snp_right)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        /// 微博正文
        textLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconImageView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-StatusCellMargin)
        }
        
        /// 模型图片
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(textLabel.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(textLabel.snp_left)
            // 设置测试图片大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        /// 原创微博底部添加约束
        self.snp_makeConstraints { (make) -> Void in
            self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
        }
        
        
        /// 将用户头像切成圆角
        iconImageView.layer.cornerRadius = 20
        iconImageView.layer.masksToBounds = true


    }
    
// MARK: 懒加载视图
    /// 用户头像
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    /// 用户名称
    private lazy var nameLabel: UILabel = UILabel(text:"啦拉拉", fontSize: 16, textColor: themeColor)

    /// 微博时间
    private lazy var timeLabel: UILabel = UILabel(text:"23:33", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    /// 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text:"来自xx的xx", fontSize: 14, textColor: UIColor.lightGrayColor())
    
    // 微博等级
    private lazy var mbrank_image: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    /// 用户认证类型
    private lazy var verified_type_image: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    /// 微博正文
    private lazy var textLabel: UILabel = UILabel(text:"这个是什么鬼--这是什么鬼--这是什么鬼", fontSize: 16, textColor: UIColor.darkGrayColor(),alignment: NSTextAlignment.Left)
    
    /// 配图视图
    private lazy var pictureView: CCStatusPictureView = CCStatusPictureView()
    
    /// sepVeiw
    private lazy var sepView: UIView = UIView()
}














